<template>
  <div class="food-content-box restaurant-list-box">
    <el-breadcrumb separator="/" class="page-title">
      <el-breadcrumb-item>Train route</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="container-padding">
      <el-form :inline="true" label-position="top" :model="searchForm" size="mini" ref="searchForm"  class="form-min-width">
        <div class="container-box label-style">
          <el-row :gutter="20">
            <el-col>

              <el-form-item :label="$t('m.Status')">
                <el-select  style="width: 200px" v-model="searchForm.status" placeholder="" clearable>
                  <el-option v-for="rg in status" :key="rg.value" :label="rg.text" :value="rg.value"></el-option>
                </el-select>
              </el-form-item>

              <span style="display:inline-block;margin-top:36px;">
                <el-button type="warning" @click="getList" size="small">{{$t('m.Search')}}</el-button>
                <el-button @click="$router.push('/routeNew')" size="small">{{$t('m.New')}}</el-button>
              </span>

            </el-col>
          </el-row>
        </div>
      </el-form>
      <!--表格-->
      <el-table
        :data="tableData"
        v-loading="tableLoading"
        border
        style="width: 100%;margin-top:20px;">
        <!--Edit Bus Line-->
        <el-table-column
          prop="Edit"
          label="Edit Bus Line"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <!--<el-button size="mini" width="40"  @click="editRestaurant(scope.id)">{{$t('m.Edit')}}</el-button>-->
            <el-button type="text" size="small" @click="editBusLine(scope.row.id)">{{$t('m.Edit')}}</el-button>
          </template>
        </el-table-column>
        <!--Status-->
        <el-table-column
          prop="Status"
          label="Status"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{getStatusItem(scope.row.status)}}</div>
          </template>
        </el-table-column>


        <!--Name_en-->
        <el-table-column
          prop="nameEn"
          label="Name_en"
          align="center"
          min-width="120">
        </el-table-column>

        <!--Name_mm-->
        <el-table-column
          prop="nameMm"
          label="Name_mm"
          align="center"
          min-width="120">
        </el-table-column>

        <!--Start Station_en-->
        <el-table-column
          prop="startStationEn"
          label="Start Station_en"
          align="center"
          min-width="130">
        </el-table-column>

        <!--Start Station_mm-->
        <el-table-column
          prop="startStationMm"
          label="Start Station_mm"
          align="center"
          min-width="140">
        </el-table-column>

        <!--End Station_en-->
        <el-table-column
          prop="endStationEn"
          label="End Station_en"
          align="center"
          min-width="140">
        </el-table-column>


        <!--End Station_mm-->
        <el-table-column
          prop="endStationMm"
          label="End Station_mm"
          align="center"
          min-width="140">
        </el-table-column>


        <!--Direction Name_en-->
        <el-table-column
          prop="directionName"
          label="Direction Name_en"
          align="center"
          min-width="240">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.directionName" style="text-align: left">
              <span  v-if="scope.row.directionName.length > 1">{{index+1}}:</span>
              {{item.name_en}}
            </div>
          </template>
        </el-table-column>


        <!--Created Date-->
        <el-table-column
          prop="CreatedDate"
          label="Created Date"
          align="center"
          min-width="160">
          <template slot-scope="scope">
            <div class="list-notes">{{getDate(scope.row.createdDate)}}</div>
          </template>
        </el-table-column>

        <!--Created By-->
        <el-table-column
          prop="CreatedBy"
          label="Created By"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.createdBy}}</div>
          </template>
        </el-table-column>
        <!--Updated Date-->
        <el-table-column
          prop="UpdatedDate"
          label="Updated Date"
          align="center"
          min-width="160">
          <template slot-scope="scope">
            <div class="list-notes">{{getDate(scope.row.updatedDate)}}</div>
          </template>
        </el-table-column>
        <!--Updated By-->
        <el-table-column
          prop="UpdatedBy"
          label="Updated By"
          align="center"
          min-width="120">
          <template slot-scope="scope">
            <div class="list-notes">{{scope.row.updatedBy}}</div>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="block" style="margin-top: 15px;text-align: center;">
        <el-pagination
          @size-change="handleChangeSize"
          @current-change="handleChangePage"
          :current-page="pageData.pageNo"
          :page-size="pageData.pageSize"
          :page-sizes="[10, 20, 50, 100, 500]"
          :page-count="pageInfo.totalPages"
          :total="pageInfo.totalCount"
          layout="total, prev, pager, next, jumper, sizes">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
  import { trainRouteQuery } from '../../../api/zxy_api'
  export default {
    name: 'BusLineIndex',
    data () {
      return {
        fileList: [],
        loading:null,
        status: [
          {"value": 1, "text": "Active"},
          {"value": 0, "text": "Deleted"},
        ],
        searchForm: {
          // 表单数据必填字段
          status: 1,
        },
        tableData: [],
        pageData: {
          pageNum: 1,
          pageSize: 20,
        },
        pageInfo: {
          totalCount: 0,
          totalPages: 0
        },
        tableLoading: false,
      }
    },
    methods: {
      handleChangeSize (val) {
        this.pageData.pageSize = parseInt(val)
        this.pageData.pageNum = 1
        this.getList()
      },
      handleChangePage (val) {
        this.pageData.pageNum = val
        this.getList()
      },
      getStatusItem (it) {
        let str = ''
        this.status.forEach((item) => {
          if (item.value == it) {
            str = item.text || ''
          }
        })
        return str
      },
      getDate (value) {
        if(!value){
          return ''
        }
        var date = new Date(value)
        var Y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        var H = date.getHours()
        var i = date.getMinutes()
        var s = date.getSeconds()
        if (m < 10) {
          m = '0' + m
        }
        if (d < 10) {
          d = '0' + d
        }
        if (H < 10) {
          H = '0' + H
        }
        if (i < 10) {
          i = '0' + i
        }
        if (s < 10) {
          s = '0' + s
        }
        var ts = Y + '-' + m + '-' + d + '  ' + H + ':' + i + ':' + s
        return ts
      },
      editBusLine (id) {
        this.$router.push({
          path: '/routeNew',
          query: {
            id: id
          }
        })
      },
      getList () {
        this.tableLoading = true
        trainRouteQuery(Object.assign(this.searchForm, this.pageData))
          .then((res) => {

            // this.showToast(res.data.errorID)
            // if (res.data.errorID === 100004) {
            //   this.$router.push('/login')
            //   return
            // }

            let data = res
            this.tableData = data.data.list
            // 分页信息配置
            this.pageInfo.totalPages = data.data.pages
            this.pageInfo.totalCount = data.data.total
            // 清除loading 状态
            setTimeout(() => {
              this.tableLoading = false
            }, 400)
          })
          .catch((error) => {
            // this.showToast(error.errorID)
            setTimeout(() => {
              this.tableLoading = false
            }, 400)
          })
      },
    },
    mounted () {
      this.getList()
    }
  }
</script>
<style scoped>
  .page-title {
    padding: 20px 40px;
    border-bottom: 1px solid #e6e6e6;
    font-size: 20px;
  }
  .food-content-box .container-padding {
    padding: 20px 40px 0 40px;
  }
  .list-notes{
    text-align: center;
    display: block;
  }
</style>
